package com.example.demo.utils

import javax.servlet.http.HttpSession
import java.util.HashMap

class MySessionContext private constructor() {
    private val sessionMap: HashMap<String, HttpSession> = HashMap()

    @Synchronized
    fun addSession(session: HttpSession?) {
        if (session != null) {
            sessionMap[session.id] = session
        }
    }

    @Synchronized
    fun delSession(session: HttpSession?) {
        if (session != null) {
            sessionMap.remove(session.id)
        }
    }

    @Synchronized
    fun getSession(sessionID: String?): HttpSession? {
        return if (sessionID == null) {
            null
        } else sessionMap[sessionID]
    }

    companion object {
        private var instance: MySessionContext? = null

        fun getInstance(): MySessionContext {
            if (instance == null) {
                instance = MySessionContext()
            }
            return instance as MySessionContext
        }
    }

}
